#pragma once

#include <string>
#include <fstream>

class Util
{
public:

    static bool readFileContent(const std::string& filename, std::string& out)
    {
        // 默认是以文本方式读取，但是图片是二进制的
        // std::ifstream in(filename);
        // if (in.is_open() == false)
        // {
        //     return false;
        // }

        // std::string line;
        // while (std::getline(in, line))
        // {
        //     out += line;
        // }
        // in.close();
        
        // return true;

        // 以二进制方式读取
        int filesize = fileSize(filename);
        if (filesize <= 0)
        {
            return false;
        }

        std::ifstream in(filename);
        if (in.is_open() == false)
        {
            return false;
        }

        out.resize(filesize);
        in.read((char*)(out.c_str()), filesize);
        in.close();

        return true;
    }

    static bool readOneLine(std::string& bigStr, std::string& out, const std::string& sep)
    {
        auto pos = bigStr.find(sep);
        if (pos == std::string::npos)
        {
            return false;
        }

        out = bigStr.substr(0, pos);
        bigStr.erase(0, pos + sep.size());
        return true;
    }

    static int fileSize(const std::string& filename)
    {
        std::ifstream in(filename, std::ios::binary);
        if (in.is_open() == false)
        {
            return -1;
        }

        in.seekg(0, in.end);
        int filesize = in.tellg();
        in.seekg(0, in.beg);
        in.close();
        return filesize;
    }

};